﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MongoDB.Driver;

namespace MongoDB
{
    public partial class formmain : Form
    {
        public formmain()
        {
            InitializeComponent();
            cbtype.SelectedIndex= 0;
        }

        private void btnpath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog()==DialogResult.OK)
            {
                txtPath.Text = fbd.SelectedPath;
                if (string.IsNullOrEmpty(txtlogpath.Text))
                {
                    txtlogpath.Text = fbd.SelectedPath + @"\logs";
                    btnnext.Enabled = true;
                }
            }
        }

        private void btnlogpath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtlogpath.Text = fbd.SelectedPath;
            }
        }

        private void btnnext_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPath.Text)||string.IsNullOrEmpty(txtlogpath.Text))
            {
                MessageBox.Show("数据库路径和日志路径不可为空！");
                return;
            }
            if (string.IsNullOrEmpty(txtlocalhost.Text)||string.IsNullOrEmpty(txtlogpath.Text))
            {
                MessageBox.Show("主机信息不可为空！");
                return;
            }
            if (!Directory.Exists(txtlogpath.Text))
            {
                Directory.CreateDirectory(txtlogpath.Text);
            }
            if (!Directory.Exists(txtPath.Text))
            {
                Directory.CreateDirectory(txtPath.Text);
            }
            List<string> comments = new List<string>();
            comments.Add(string.Format("dbpath={0}", txtPath.Text));
            comments.Add(string.Format(@"logpath={0}\mongodb.log", txtlogpath.Text));
            comments.Add("logappend=false");//日志写入模式
            comments.Add("directoryperdb=true");//文件存储模式
            comments.Add(string.Format("port={0}",txtport.Text));

            //ReplicaSet Master-Slave
            if (cbtype.Text == "ReplicaSet")
            {
                ReplicaSet rs = new ReplicaSet(this, comments);
                rs.Show();
                this.Hide();
            }
            else if (cbtype.Text == "Master-Slave")
            {
                MasterSlave ms = new MasterSlave(this, comments);
                ms.Show();
                this.Hide();
            }
            else
            {
                var filepath = string.Format(@"{0}\mongodb.cfg", Application.StartupPath);
                File.Delete(filepath);
                File.AppendAllLines(filepath, comments, Encoding.ASCII);
                InstallServer iis = new InstallServer(this, filepath, null);
                iis.Show();
                this.Hide();
            }
        }
    }
}
